package com.humngyang.com.hmy.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.humngyang.com.hmy.entity.Goods;
import com.humngyang.com.hmy.entity.User;
import com.humngyang.com.hmy.mapper.GoodsMapper;
import com.humngyang.com.hmy.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class GoodsService {

    @Autowired
    GoodsMapper goodsMapper;


    public Map<String, Object> getAllGoods(Map<String, Object> prames) {
        Map<String, Object> re = new HashMap<>();
        Integer pagesize = (Integer) prames.get("pagesize");
        Integer current = (Integer) prames.get("current");

        QueryWrapper queryWrapper = new QueryWrapper();
        String name = (String) prames.get("name");
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        } else {
            queryWrapper = null;
        }
        Page<Goods> page = new Page<>(current, pagesize);
        IPage<Goods> users = goodsMapper.selectPage(page, queryWrapper);
        if (CollectionUtils.isEmpty(users.getRecords())) {
            re.put("flag", 1);
            re.put("list", Collections.EMPTY_LIST);
            return re;
        }
        re.put("flag", 1);
        re.put("list", users);
        return re;
    }

    /**
     * Author : hmy
     *
     * @date 2020/3/3 10:15
     * describution : 添加更新商品信息
     **/
    public void addgoods(Goods goods) {
        if (StringUtils.isEmpty(goods.getId())) {
            goods.setId(UUID.randomUUID().toString().replace("-", ""));
            goods.setCreatedate(DateUtil.format(new Date(), "yyyy-MM-dd"));
            goodsMapper.insert(goods);
        } else {
            goodsMapper.updateById(goods);
        }
    }

    /**
     * Author : hmy
     *
     * @date 2020/3/3 12:29
     * describution : 根据id删除商品
     **/

    public void delateByGoodId(String id) {
        goodsMapper.deleteById(id);
    }


}
